<template>
	<el-dialog 
	:title="!dataForm.userId ? '新增' : '修改'" 
		:close-on-click-modal="false" 
		:close-on-press-escape="false" 
		:show-close="false" 
		:visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="请输入名称" maxlength="20" clearable></el-input>
			</el-form-item>
			<el-form-item label="宽度" prop="width">
				<el-input-number v-model="dataForm.width" controls-position="right" :min="0" label="宽度" type="number"></el-input-number>
			</el-form-item>
			<el-form-item label="高度" prop="height">
				<el-input-number v-model="dataForm.height" controls-position="right" :min="0" label="高度" type="number"></el-input-number>
			</el-form-item>
			<el-form-item label="描述" prop="descr">
				<el-input v-model="dataForm.descr" placeholder="请输入描述" maxlength="50" clearable></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel()">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					id: 0,
					name: '',
					width: 0,
					height: 0,
					descr: ''
				},
				dataRule: {
					name: [{
						required: true,
						message: '名称不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl(`/back/adPosition/info/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({
						data
					}) => {
						if (data && data.code === 0) {
							this.dataForm.name = data.data.name
							this.dataForm.width = data.data.width
							this.dataForm.height = data.data.height
							this.dataForm.descr = data.data.descr
						}
					})
				}
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.$http({
							url: this.$http.adornUrl(`/back/adPosition/${!this.dataForm.id ? 'save' : 'update'}`),
							method: 'post',
							data: this.$http.adornData({
								'id': this.dataForm.id || undefined,
								'name': this.dataForm.name,
								'width': this.dataForm.width,
								'height': this.dataForm.height,
								'descr': this.dataForm.descr
							})
						}).then(({
							data
						}) => {
							if (data && data.code === 0) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.visible = false
										this.$emit('refreshDataList')
									}
								})
							} else {
								this.$message.error(data.msg)
							}
						})
					}
				})
			},
			// 取消操作
			cancel () {
				this.$emit('closeDialog')
				this.visible = false
			},
		}
	}
</script>
